package com.jiuwu.spring.eshop.controller;

import com.jiuwu.spring.eshop.db.entity.Member;
import com.jiuwu.spring.eshop.respository.MemberRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;


import java.util.List;
import java.util.Optional;

@Slf4j
@Controller
@RequestMapping("/manage")
public class MemberController {

    @Autowired
    MemberRepository memberRepository;

    /**
     * ?????????????
     */
    @RequestMapping("/member_manage")
    public String getMemberList(Model model) {
        List<Member> member = memberRepository.findAll();
        model.addAttribute("memberLst", member);
        return "/manage/member_manage";
    }

    /**
     * ??????????
     */
    @RequestMapping("/member_detail/{memberID}")
    public String getMember(@PathVariable("memberID") Integer memberID, Model model) {
        Optional<Member> memberOptional = memberRepository.findById(memberID);
        Member member = null;
        if (memberOptional.isPresent()) {
            member = memberOptional.get();
        }
        model.addAttribute("member", member);
        return "/manage/member_detail";
    }

    /**
     * ????/????????
     */
    @RequestMapping("/member_freeze/{memberId}/{freeze}")
    public String freezeMember(@PathVariable(value = "memberId", required = false) String memberIdStr,
                               @PathVariable(value = "freeze", required = false) String freezeStr) {
        Integer memberId = Integer.valueOf(memberIdStr);
        Integer freeze = Integer.valueOf(freezeStr);
        memberRepository.updateMemberInfo(memberId, freeze);

        return "redirect:/manage/index?id=2";
    }



//------------------------------------thymeleaf-------------------------------------

    /**
     *  获取所有会员信息
     */
    @GetMapping(value = {"/members/{pageNum}","/members"})
    public String getMembers(@PathVariable(value = "pageNum",required = false) Integer pageNum,Model model) {
        pageNum = pageNum == null ? 0 : pageNum>0?pageNum-1:0;
        Page<Member> memberPage = memberRepository.findAll(PageRequest.of(pageNum , 3));
        model.addAttribute("memberPage", memberPage);
        for (Member member : memberPage.getContent()) {
            System.out.println("member:........." + member);
        }
        return "member";
    }

    @GetMapping("/member/{memberId}")
    public String getMember2(@PathVariable("memberId")Integer memberId,Model model){
        Optional<Member> memberOptional = memberRepository.findById(memberId);
        Member member = null;
        if (memberOptional.isPresent()) {
            member = memberOptional.get();
        }
        model.addAttribute("member", member);
        return "member/queryMember";
    }

}